package com.ycplayerlib;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.ycplayerlib.listener.YCOnChangeSize;
import com.ycplayerlib.listener.YCOnCompleteListener;
import com.ycplayerlib.listener.YCOnCutVideoImgListener;
import com.ycplayerlib.listener.YCOnErrorListener;
import com.ycplayerlib.listener.YCOnInfoListener;
import com.ycplayerlib.listener.YCOnLoadListener;
import com.ycplayerlib.listener.YCOnPreparedListener;
import com.ycplayerlib.opengles.YCGlSurfaceView;
import com.ycplayerlib.util.MyLog;
import com.ycplayerlib.ycplayer.YCPlayer;
import com.ycplayerlib.ycplayer.YCTimeBean;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class TestVideoActivity extends AppCompatActivity {
    private YCGlSurfaceView surfaceview;
    private YCPlayer ycPlayer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_test_video);
        surfaceview = findViewById(R.id.surfaceview);
        ycPlayer = new YCPlayer();
        ycPlayer.setOnlyMusic(false);
        String pathurl = "http://testimg.dcpai.cn/8B4BC688-8739-41A7-9044-2B427FC8428D.mp4";
//        String pathurl =  Environment.getExternalStorageDirectory()+"/a8.mp4";
        Log.e("www",pathurl);



        ycPlayer.setDataSource(pathurl);
        ycPlayer.setOnlySoft(true);
        ycPlayer.setycGlSurfaceView(surfaceview);

        ycPlayer.setycOnErrorListener(new YCOnErrorListener() {
            @Override
            public void onError(int code, String msg) {
                MyLog.d("code:" + code + ",msg:" + msg);
                Message message = Message.obtain();
                message.obj = msg;
                message.what = 3;
                handler.sendMessage(message);
            }
        });

        ycPlayer.setycOnPreparedListener(new YCOnPreparedListener() {
            @Override
            public void onPrepared() {
                MyLog.d("starting......");
                ycPlayer.start();
            }
        });

        ycPlayer.setycOnLoadListener(new YCOnLoadListener() {
            @Override
            public void onLoad(boolean load) {
                MyLog.d("loading ......>>>   " + load);
                Message message = Message.obtain();
                message.what = 1;
                message.obj = load;
                handler.sendMessage(message);
            }
        });

        ycPlayer.setycOnInfoListener(new YCOnInfoListener() {
            @Override
            public void onInfo(YCTimeBean wlTimeBean) {
                Message message = Message.obtain();
                message.what = 2;
                message.obj = wlTimeBean;
                MyLog.d("nowTime is " +wlTimeBean.getCurrt_secds());
                handler.sendMessage(message);
            }
        });

        ycPlayer.setycOnCompleteListener(new YCOnCompleteListener() {
            @Override
            public void onComplete() {
                MyLog.d("complete .....................");
                ycPlayer.stop(true);
            }
        });

        ycPlayer.setycOnCutVideoImgListener(new YCOnCutVideoImgListener() {
            @Override
            public void onCutVideoImg(Bitmap bitmap) {
                Message message = Message.obtain();
                message.what = 4;
                message.obj = bitmap;
                handler.sendMessage(message);
            }
        });

        ycPlayer.setChangeSize(new YCOnChangeSize() {
            @Override
            public void onViewSize(int width, int height) {
                a(width,height);
            }
        });

        ycPlayer.prepared();
    }

    void a(final int width, final int height){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) surfaceview.getLayoutParams();
                layoutParams.width = width;
                layoutParams.height = height;
                surfaceview.setLayoutParams(layoutParams);
            }
        });
    }

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1)
            {
//                boolean load = (boolean) msg.obj;
//                if(load)
//                {
//                    progressBar.setVisibility(View.VISIBLE);
//                }
//                else
//                {
//                    if(lyAction.getVisibility() != View.VISIBLE)
//                    {
//                        lyAction.setVisibility(View.VISIBLE);
//                        ivCutImg.setVisibility(View.VISIBLE);
//                    }
//                    progressBar.setVisibility(View.GONE);
//                }
            }
            else if(msg.what == 2)
            {
//                WlTimeBean wlTimeBean = (WlTimeBean) msg.obj;
//                if(wlTimeBean.getTotal_secds() > 0)
//                {
//                    seekBar.setVisibility(View.VISIBLE);
//                    if(isSeek)
//                    {
//                        seekBar.setProgress(position * 100 / wlTimeBean.getTotal_secds());
//                        isSeek = false;
//                    }
//                    else
//                    {
//                        seekBar.setProgress(wlTimeBean.getCurrt_secds() * 100 / wlTimeBean.getTotal_secds());
//                    }
//                    tvTime.setText(WlTimeUtil.secdsToDateFormat(wlTimeBean.getTotal_secds()) + "/" + WlTimeUtil.secdsToDateFormat(wlTimeBean.getCurrt_secds()));
//                }
//                else
//                {
//                    seekBar.setVisibility(View.GONE);
//                    tvTime.setText(WlTimeUtil.secdsToDateFormat(wlTimeBean.getCurrt_secds()));
//                }
            }
            else if(msg.what == 3)
            {
//                String err = (String) msg.obj;
//                Toast.makeText(VideoLiveActivity.this, err, Toast.LENGTH_SHORT).show();
            }
            else if(msg.what == 4)
            {
//                Bitmap bitmap = (Bitmap) msg.obj;
//                if(bitmap != null)
//                {
//                    ivShowImg.setVisibility(View.VISIBLE);
//                    ivShowImg.setImageBitmap(bitmap);
//                }
            }
        }
    };

    @Override
    public void onBackPressed() {
        if(ycPlayer != null)
        {
            ycPlayer.stop(true);
        }
        this.finish();
    }
}
